package com.turtle.security.security.handle;

import com.alibaba.fastjson.JSON;
import com.turtle.security.common.constant.HttpStatus;
import com.turtle.security.common.utils.ApiResult;
import com.turtle.security.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证验证失败处理类
 *
 * @author turtle
 * date: 2020-05-06
 */
@Component
@Slf4j
public class AuthenticationFailHandle implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {

        ServletUtils.renderString(httpServletResponse, JSON.toJSONString(ApiResult.unauthorized(HttpStatus.UNAUTHORIZED, "认证失败，请重新登录")));
    }
}
